from datetime import datetime

from sqlalchemy_serializer import SerializerMixin

from app import dBSession
from app.Models.BaseModel import BaseModel
from app.Models.Model import SapMaterialDetailsInfo
from app.Vendor.Decorator import classTransaction


class SapMaterialDetailsInfos(SapMaterialDetailsInfo, BaseModel, SerializerMixin):

    @classTransaction
    def get_one(self, material):
        res = dBSession.query(SapMaterialDetailsInfo).filter(SapMaterialDetailsInfo.id == material.id).first()
        if not res:
            return None
        res = res.to_dict()
        return res

    # 增加
    @classTransaction
    def add(self, material):
        material.created_at = datetime.now()
        dBSession.add(material)
        dBSession.flush()
        return material.id

    @classTransaction
    def add_list(self, material_list):
        if not isinstance(material_list, list):
            return None
        if not material_list:
            return None
        if len(material_list) == 0:
            return None
        idnrk_list = []
        vehicle_part = material_list[0].matnr
        werks = material_list[0].werks
        for material in material_list:
            material.created_at = datetime.now()
            idnrk_list.append(material.idnrk)
        exist_material = dBSession.query(SapMaterialDetailsInfo).filter(SapMaterialDetailsInfo.idnrk.in_(idnrk_list),
                                                                        SapMaterialDetailsInfo.matnr == vehicle_part,
                                                                        SapMaterialDetailsInfo.werks == werks).all()
        exist_idnrk_list = []
        new_material_list = []
        if len(exist_material) > 0:
            for material in material_list:
                exist_idnrk_list.append(material.idnrk)
            for material in material_list:
                if material.idnrk not in exist_idnrk_list:
                    new_material_list.append(material)
        else:
            new_material_list = material_list
        dBSession.add_all(new_material_list)
        dBSession.flush()
        return material_list

    @classTransaction
    def edit(self, material):
        update_data = {"updated_by": material.updated_by, "updated_at": datetime.now()}
        if material.werks:
            update_data["werks"] = material.werks
        if material.matnr:
            update_data["matnr"] = material.matnr
        if material.maktx1:
            update_data["maktx1"] = material.maktx1
        if material.idnrk:
            update_data["idnrk"] = material.idnrk
        if material.maktx2:
            update_data["maktx2"] = material.maktx2
        if material.menge:
            update_data["menge"] = material.menge
        if material.text1:
            update_data["text1"] = material.text1
        if material.text2:
            update_data["text2"] = material.text2
        if material.text3:
            update_data["text3"] = material.text3
        if material.text4:
            update_data["text4"] = material.text4
        if material.text5:
            update_data["text5"] = material.text5
        if material.text6:
            update_data["text6"] = material.text6

        dBSession.query(SapMaterialDetailsInfo).filter(SapMaterialDetailsInfo.id == material.id) \
            .update(update_data, synchronize_session=False)
        return True

    # 根据id删除
    @classTransaction
    def delete_by_id(self, material):
        update_data = {"updated_by": material.updated_by, "updated_at": datetime.now(), "is_delete": 1}
        dBSession.query(SapMaterialDetailsInfo).filter(SapMaterialDetailsInfo.id == material.id).update(update_data)
        return True

    @classTransaction
    def get_list(self, filters: set, offset: int = 0, limit: int = 15):
        res = {}
        res['page'] = {}
        res['page']['count'] = dBSession.query(SapMaterialDetailsInfo).filter(*filters).count()
        res['list'] = []
        res['page']['total_page'] = self.get_page_number(res['page']['count'], limit)
        res['page']['current_page'] = offset
        if offset != 0:
            offset = (offset - 1) * limit
        if res['page']['count'] > 0:
            res['list'] = dBSession.query(SapMaterialDetailsInfo).filter(*filters).offset(offset).limit(limit).all()
            res['list'] = [c.to_dict() for c in res['list']]
        return res

    @classTransaction
    def get_one_by_vehicle_part_id(self, vehicle_part_id):
        res = dBSession.query(SapMaterialDetailsInfo).filter(
            SapMaterialDetailsInfo.vehicle_part_id == vehicle_part_id).all()
        return res
